package mn.idax.exchange.otc.web;

import com.google.common.collect.Maps;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mn.idax.exchange.common.bean.ResponseData;
import mn.idax.exchange.otc.constant.RequestUrlConstant;
import mn.idax.exchange.otc.enums.response.SystemStatusResponseEnum;
import mn.idax.exchange.otc.exception.IdaxException;
import mn.idax.exchange.otc.service.BusinessApplyService;
import mn.idax.exchange.otc.vo.PageRequestDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Auther: .jension
 * @Date: 2018/11/13
 * @Description:
 */

@Api(value = "商家", tags = {"商家"})
@RequestMapping(RequestUrlConstant.MODULE_BUSINESS)
@RestController
public class BusinessApplyController extends BaseController {

    @Autowired
    private BusinessApplyService businessApplyService;

    /**
     * 查询商家申请条件
     */
    @ApiOperation(value = "查询商家申请条件", notes = "查询商家申请条件")
    @RequestMapping(value = RequestUrlConstant.BUSINESS_CONDITION, method = RequestMethod.POST)
    public ResponseData getBusinessCondition() {
        return ResponseData.success(businessApplyService.getBusinessCondition(getUserId()));
    }

    /**
     * 申请
     *
     * @return
     */
    @ApiOperation(value = "申请商家", notes = "申请商家")
    @RequestMapping(value = RequestUrlConstant.BUSINESS_APPLY, method = RequestMethod.POST)
    public ResponseData apply() throws Exception {
        Integer userId = getUserId();
        // 提交申请
        int isApplySuccess = businessApplyService.submitApply(userId);
        if (isApplySuccess == 0) {
            throw new IdaxException(SystemStatusResponseEnum.FAIL.getCode());
        }
        return ResponseData.success();
    }

    /**
     * 获取商家申请列表
     *
     * @param pageRequest
     * @return
     */
    @ApiOperation(value = "申请列表", notes = "申请列表")
    @RequestMapping(value = RequestUrlConstant.BUSINESS_LIST, method = RequestMethod.POST)
    public ResponseData getBusinessApplylist(@RequestBody PageRequestDTO pageRequest) {
        Map<String, Integer> paramMap = Maps.newHashMap();
        Integer pageIndex = pageRequest.getPageIndex();
        Integer pageSize = pageRequest.getPageSize();
        paramMap.put("pageIndex", (pageIndex == null || pageIndex < 0) ? 0 : pageIndex);
        paramMap.put("pageSize", (pageSize == null || pageSize <= 0 || pageSize > 1000) ? 1000 : pageSize);
        paramMap.put("userId", getUserId());
        return ResponseData.success(businessApplyService.getBusinessListByUserId(paramMap));
    }


}